﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
using LayoutPlanning.Helpers;

namespace LayoutPlanning.Model
{
    [Serializable]
    public class GraficalStorageSpace : GraficalObject
    {

        private Point _endPoint;
        private Point _startPoint;
        private Product _selectedProduct;
        private ObservableCollection<Product> _products = new ObservableCollection<Product>();


        public GraficalStorageSpace(Point startPoint, Point endPoint)
        {
            _startPoint = startPoint;
            _endPoint = endPoint;

            
            InitRectangel();
        }

        public ObservableCollection<Product> Products
        {
            get { return _products; }
        }

        public Product SelectedProduct
        {
            get { return _selectedProduct; }
            set
            {
                _selectedProduct = value;
                RaisePropertyChanged();
            }
        }

        public RelayCommand AddProduct
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var p = new Product()
                    {
                        GraficFile = "",
                        Weight = 0d
                    };
                    SetProductName(p, "Product");
                    Products.Add(p);
                });
            }
        }



        public void SetProductName(Product p, string value)
        {
            int i = 0;
            while (Products.Any(t => t.Name == (i > 0 ? value + i : value)))
                i++;
            if (i > 0)
                value += i;
            p.Name = value;
        }

        public override async Task<GraficalObject> Copy()
        {
            return await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
            {
                var gs = new GraficalStorageSpace(_endPoint, _startPoint);
                {
                }
                ;
                foreach (var product in Products)
                {
                    gs.Products.Add(product);
                }
                return gs;
            }, DispatcherPriority.ContextIdle);
        }

        protected GraficalStorageSpace(SerializationInfo info, StreamingContext context)
        {
           _startPoint = (Point)info.GetValue("StartPoint", typeof(Point));
            _endPoint = (Point)info.GetValue("EndPoint", typeof(Point));
            _products = (ObservableCollection<Product>) info.GetValue("Products", typeof(ObservableCollection<Product>));

            InitRectangel();
        }

        private void InitRectangel()
        {

            //todo:Remove
          
            var x = Math.Min(_endPoint.X, _startPoint.X);
            var y = Math.Min(_endPoint.Y, _startPoint.Y);

            var w = Math.Max(_endPoint.X, _startPoint.X) - x;
            var h = Math.Max(_endPoint.Y, _startPoint.Y) - y;

           
            ControlType = ControlType.Square;

            SizeX = w;
            SizeY = h;
            
        }


        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
           info.AddValue("StartPoint", _startPoint);

           info.AddValue("EndPoint", _endPoint);

            info.AddValue("Products", Products);
        }
    }
}